package com.hmkj.core.mapper.account;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.account.AccountLogModel;
import com.hmkj.core.po.account.AccountLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 资金记录相关Dao接口<br>
 *
 * @author eproo
 * @date 2018-07-05 09:50:54
 */
public interface AccountLogMapper extends GenericMapper<AccountLog, Integer> {
    @Override
    @Select("SELECT count(1) FROM biz_account_log t LEFT JOIN biz_user u ON t.user_id = u.id  WHERE 1=1 ${whereSql} ${sortSql} ")
    long countBySqlJoin(Map<String, Object> params);

    @Override
    @Select("SELECT t.*, u.user_name AS 'u.userName',u.mobile_phone AS 'u.mobilePhone' FROM biz_account_log t LEFT JOIN biz_user u ON t.user_id = u.id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<AccountLogModel> findListBySqlJoin(Map<String, Object> params);

    /**
     * 前台资金日志列表
     *
     * @param param
     * @return
     */
    @Select(" <script> SELECT t.* " +
            "FROM biz_account_log t WHERE t.user_id = #{userId}  AND t.del_flag = 1 " +
            "<if test=\"type != null and type != ''\"> AND t.`classify` = #{type} </if>" +
            "<if test=\"startTime != null and startTime != ''\"> AND t.`create_time` >= #{startTime} </if>" +
            "<if test=\"endTime != null and endTime != ''\"> AND t.`create_time` &lt;= #{endTime} </if>" +
            "ORDER BY t.create_time DESC,t.id DESC</script>")
    List<AccountLog> findListForAccountLogList(Map<String, Object> param);

    /**
     * 前台资金日志列表
     *
     * @param param
     * @return
     */
    @Select(" <script> SELECT count(*) " +
            "FROM biz_account_log t WHERE t.user_id = #{userId}  AND t.del_flag = 1 " +
            "<if test=\"type != null and type != ''\"> AND t.`classify` = #{type} </if>" +
            "<if test=\"startTime != null and startTime != ''\"> AND t.`create_time` >= #{startTime} </if>" +
            "<if test=\"endTime != null and endTime != ''\"> AND t.`create_time` &lt;= #{endTime} </if>" +
            "</script>")
    Integer findCountForAccountLogList(Map<String, Object> param);


    /**
     * 根据类型统计用户操作金额
     *
     * @param param 操作类型
     * @return
     */
    @Select("SELECT sum(account) FROM biz_account_log WHERE 1=1 and type in ${whereSql} and del_flag = 1")
    BigDecimal selectSumByType(Map<String, Object> param);


    /**
     * 根据类型和资金流向统计操作金额
     *
     * @param direction 资金流向
     * @param type      操作类型
     * @param userId    用户ID
     * @return
     */
    @Select("SELECT sum(account) FROM biz_account_log WHERE 1=1 and direction = #{direction} and type= #{type} and user_id = #{userId} and del_flag = 1")
    BigDecimal selectSumByAirectionType(@Param(value = "direction") int direction, @Param(value = "type") int type, @Param(value = "userId") Integer userId);


    /**
     * 根据类型查询所有操作资金
     *
     * @param param
     * @return
     */
    @Select("<script> SELECT sum(t.account) FROM biz_account_log t LEFT JOIN biz_user user ON user.id = t.user_id  WHERE 1=1 and t.type= #{type} and t.del_flag = 1 " +
            "<if test=\"startTime != null and startTime != ''\"> AND t.`create_time` >= #{startTime} </if>" +
            "<if test=\"endTime != null and endTime != ''\"> AND t.`create_time` &lt;= #{endTime} </if></script>")
    BigDecimal selectSumAmountByType(Map<String, Object> param);

    /**
     * 根据时间和类型统计次数
     */
    @Select("SELECT count(1) FROM biz_account_log t WHERE 1 = 1  AND t.type in (#{type},#{type2}) AND t.del_flag = #{delFlag} AND t.user_id = #{userId} AND t.create_time like CONCAT(date_format(#{createTime},'%Y-%m-%d'),'%')")
    Integer countByTime(Map<String, Object> param);

    /**
     * 根据时间和类型统计总额
     */
    @Select("SELECT sum(t.account) FROM biz_account_log t WHERE 1 = 1  AND t.type in (#{type},#{type2}) AND t.del_flag = #{delFlag} AND t.user_id = #{userId} AND t.create_time like CONCAT(date_format(#{createTime},'%Y-%m-%d'),'%')")
    BigDecimal sumAmountByTime(Map<String, Object> param);

    @Select("SELECT * FROM biz_account_log t WHERE t.del_flag = 1 AND t.user_id in (#{userIds}) AND  t.create_time >= #{startTime} AND t.create_time <= #{endTime} AND  t.type =  #{type} AND t.direction = 1 ")
    List<AccountLog> selectAccountLogWithTime(Map<String, Object> param);

    @Select("SELECT * FROM biz_account_log t WHERE t.del_flag = 1 AND  t.create_time >= #{startTime} AND t.create_time <= #{endTime} AND  t.type =  #{type} AND t.direction = 1 ")
    List<AccountLog> selectAccountLogAllWithTime(Map<String, Object> param);
}
